home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / fioben.zip / CAT1.CPP < prev    next >
C/C++ Source or Header  |  1993-02-09  |  3KB  |  126 lines

  1. #include    <windows.h>
  2. #include    <iostream.h>
  3. #include    <iomanip.h>
  4. #include    <fstream.h>
  5. #include    <string.h>
  6.  
  7. ofstream    CERR("results.out", ios::app);
  8. int    BUFLEN = 0x10000;
  9.  
  10. ostream    & operator << (ostream & os, SYSTEMTIME & r)
  11. {
  12.     static    const char    * const rgszDayNames[] =
  13.         {
  14.         "Sunday",
  15.         "Monday",
  16.         "Tuesday",
  17.         "Wednesday",
  18.         "Thursday",
  19.         "Friday",
  20.         "Saturday",
  21.         0
  22.         };
  23.     char    chFill = os.fill();
  24.     os.fill('0');
  25.     os << rgszDayNames[r.wDayOfWeek] << ' '
  26.         << setw(2) << r.wMonth << '/'
  27.         << setw(2) << r.wDay << '/'
  28.         << setw(2) << r.wYear << ' '
  29.         << setw(2) << r.wHour << ':'
  30.         << setw(2) << r.wMinute << ':'
  31.         << setw(2) << r.wSecond;
  32.     os.fill(chFill);
  33.     return    os;
  34. }
  35.  
  36. void    cat2(HANDLE h, HANDLE hOut)
  37. {
  38.     HANDLE    hMap = CreateFileMapping(h, 0, PAGE_READONLY, 0,0, 0);
  39.     if (0 == hMap)
  40.         {
  41.         CERR << GetLastError() << ": error in CreateFileMapping." << endl;
  42.         return;
  43.         }
  44.     LPVOID    pView = MapViewOfFile(hMap, FILE_MAP_READ, 0,0, 0);
  45.     if (0 == pView)
  46.         {
  47.         CERR << GetLastError() << ": error in MapViewOfFile()." << endl;
  48.         CloseHandle(hMap);
  49.         return;
  50.         }
  51.     DWORD    dwNothing, dwFileSizeLo = GetFileSize(h, &dwNothing);
  52.     const    char    *pOut = (char *)pView;
  53.     while (dwFileSizeLo)
  54.         {
  55.         if (!WriteFile(hOut, (const void *)pOut, dwFileSizeLo, &dwNothing, 0))
  56.             {
  57.             CERR << GetLastError() << ": error in WriteFile()." << endl;
  58.             break;
  59.             }
  60.         dwFileSizeLo -= dwNothing;
  61.         pOut += dwNothing;
  62.         }
  63.     UnmapViewOfFile(pView);
  64.     CloseHandle(hMap);
  65. }
  66. void cat(HANDLE h, HANDLE hOut)
  67. {
  68.     const    int    BUFLEN = 0x10000;
  69.     char    *szBuf = new char[BUFLEN];
  70.     DWORD    nBytes;
  71.     while (ReadFile(h, szBuf, BUFLEN, &nBytes, 0) && nBytes)
  72.         {
  73.         DWORD    nWritten;
  74.         if (!WriteFile(hOut, szBuf, nBytes, &nWritten, 0) )
  75.             {
  76.             CERR << GetLastError() << ": error in WriteFile. "
  77.                 << nBytes << " read, " << nWritten << " written."
  78.                 << endl;
  79.             // return;
  80.             }
  81.         }
  82.     delete [] szBuf;
  83. }
  84.  
  85. int main(int argc, char **argv)
  86. {
  87.     SYSTEMTIME    sNow;
  88.     GetLocalTime(&sNow);
  89.     CERR << sNow << "===> " << GetCommandLine() << endl;
  90.  
  91.     HANDLE    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  92.     if (INVALID_HANDLE_VALUE == hStdout)
  93.         {
  94.         CERR << GetLastError() << ": error opening con for writing."
  95.             << endl;
  96.         return    4;
  97.         }
  98.     if (argc < 2)
  99.         {
  100.         cat(GetStdHandle(STD_INPUT_HANDLE), hStdout);
  101.         return    0;
  102.         }
  103.     DWORD    dwStart = GetTickCount();
  104.     for (int iArg = 1; iArg < argc; iArg++)
  105.         {
  106.         HANDLE    fp = CreateFile(argv[iArg], GENERIC_READ,
  107.             FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
  108.             OPEN_EXISTING, 0, 0);
  109.         if (INVALID_HANDLE_VALUE == fp)
  110.             {
  111.             CERR << GetLastError() << ": error opening file "
  112.                 << argv[iArg] << " for reading." << endl;
  113.             continue;
  114.             }
  115.  
  116.         cat(fp, hStdout);
  117.         CloseHandle(fp);
  118.         }
  119.     dwStart = GetTickCount( ) - dwStart;
  120.      CERR << "**** Total time: " << (dwStart / 1000)
  121.         << '.' << (dwStart % 1000) << " seconds."
  122.         << endl;
  123.     return    0;
  124. }
  125.  
  126.